package com.cskaoyan.market.admin.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminIssueService;
import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @Author:TaoRencan
 * @Date: 2023/3/21 10:21
 */
public class MarketAdminIssueServiceImpl implements MarketAdminIssueService {
    @Override
    public void delete(String addTime, String answer, String deleted, String id, String question, String updateTime) {
        SqlSession session = MyBatisUtil.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssue marketIssue = mapper.selectByPrimaryKey(Integer.parseInt(id));
        marketIssue.andLogicalDeleted(true);
        marketIssue.setDeleted(true);
        marketIssue.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketIssue);
        //这次是因为和方文详的list 概念不同 逻辑删除和物理删除的区别 现有delete（逻辑删除）要对应list的物理删除显示
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();

    }

    @Override
    public List<MarketIssue> getList(Integer page, Integer limit, String sort, String order, String question) {
        SqlSession session = MyBatisUtil.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        if (!StringUtils.isEmpty(question)) {
            criteria.andQuestionLike("%"+question+"%");
        }
        issueExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketIssue> marketIssueList = mapper.selectByExample(issueExample);
        session.commit();
        session.close();
        return marketIssueList;
    }

    @Override
    public MarketIssue createIssue(String answer, String question) {
        SqlSession session = MyBatisUtil.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setAddTime(LocalDateTime.now());
        marketIssue.setAnswer(answer);
        marketIssue.setId(marketIssue.getId());
        marketIssue.setQuestion(question);
        marketIssue.setUpdateTime(LocalDateTime.now());
        mapper.insertSelective(marketIssue);
        session.commit();
        session.close();
        return marketIssue;
    }

    @Override
    public MarketIssue updateIssue(String answer, String question, String addTime, String deleted, Integer id, String updateTime) {
        SqlSession session = MyBatisUtil.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        MarketIssue marketIssue = mapper.selectByPrimaryKey(id);
        marketIssue.setAddTime(LocalDateTime.parse(addTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        marketIssue.setAnswer(answer);
        marketIssue.setQuestion(question);
        marketIssue.setUpdateTime(LocalDateTime.now());
        mapper.updateByPrimaryKey(marketIssue);
        session.commit();
        session.close();
        return marketIssue;
    }
}